//index.js
//获取应用实例
const app = getApp();
import HttpRequst from '../../utils/httpRequst.js'
import API from '../../utils/config.js'

Page({
  data: {
    swiperImg:{
      lists: [],
      indicatorDots: true,
      autoplay: true,
      interval: 3000,
      duration: 500
    },
    swiperText: {
      lists: [],
      vertical:true,
      indicatorDots: false,
      autoplay: true,
      interval: 3000,
      duration: 250
    },
    tabType:1,
    page:1,
    pagesize:10,
    isLoading:true,
    isEnding:false,
    lists:[],

    capital: ""
  },
  
  onLoad: function () {
    var that=this;
    if (!app.getUkey()) {
      wx.redirectTo({
        url: '/pages/login/login',
      })
    }else{
      HttpRequst(false, API.index, 1, app.getUkey(), { page: this.data.page, pagesize: this.data.pagesize }, "POST", false, function (res) {
        that.setData({
          isLoading: false,
          ['swiperImg.lists']:res.data.banner || "",
          ['swiperText.lists']: res.data.news || "",
          lists: res.data.lists,
          page: that.data.page + 1,
          isEnding: (res.data.lists.length < that.data.pagesize) ? true : false
        })
      }, "index");

      HttpRequst(true, API.capitalTotal, 1, app.getUkey(), "", "POST", false, function (res) {
        that.setData({
          capital: res.data
        })
      })
    }

    var arrSwipeText=[];
    for (var i = 0; i < this.data.swiperText.lists.length; i++) {
      arrSwipeText[i] = { 
        name: this.data.swiperText.lists[i].name.substring(0,1) + "**",
        amount: this.data.swiperText.lists[i].amount
      };
      
    }
   
    this.setData({
      ['swiperText.lists']: arrSwipeText
    })

  },
  onReachBottom: function () {
    if (this.data.isEnding) return;
    var that = this;
    this.setData({
      isLoading: true
    });
    HttpRequst(false, API.index, 1, "", { page: this.data.page, pagesize: this.data.pagesize }, "POST", false, function (res) {
      that.setData({
        isLoading: false,
        lists: that.data.lists.concat(res.data.lists),
        page: that.data.page + 1,
        isEnding: (res.data.lists.length < that.data.pagesize) ? true : false
      })
    })
  },
  toggleSwitchTab:function(event){
    var tabType = event.currentTarget.dataset.tab;
    this.setData({
      tabType: tabType
    })
  },
  toggleStatus: function () {
    HttpRequst(true, API.tixianstatus, 1, app.getUkey(), "", "POST", false, function (res) {
      if (res.data.status == 0 || !res.data.status) {
        wx.navigateTo({
          url: '/pages/settingAccountIntro/settingAccountIntro?mobile=' + res.data.mobile,
        })
      } else {
        wx.navigateTo({
          url: '/pages/tiXian/tiXian',
        })
      }
    })
  },
  loginout: function () {
    app.logout();
  },

  bannerDirect:function(e){
   console.log(e);
    var idx=e.currentTarget.dataset.idx;
   
    var item=this.data.swiperImg.lists[idx];
    
    if(!!item.url){
      if (item.url == "redirect") {
        wx.navigateTo({
          url: '/pages/bannerDetail/bannerDetail?id=' + item.id
        })
       
      } else {
        wx.navigateTo({
          url: '/pages/webViewBanner/webViewBanner?url=' + item.url,
        })
      }
      
    }
    

  }
  
})
